package com.example.gw_back_end.controller;

import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.domain.entities.Post;
import com.example.gw_back_end.service.PostService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("post")
public class PostController {

    private final PostService postService;
    public PostController(PostService postService) {
        this.postService = postService;
    }

    // 获取帖子列表
    @GetMapping("get_post_list")
    public Result getPostList(PageParam pageParam){
        return Result.success(postService.getPostList(pageParam));
    }

    // 新增帖子
    @PostMapping("add_post")
    public Result addPost(@RequestBody Post post){
        if (postService.addPost(post)) {
            return Result.success();
        } else {
            return Result.error("新增帖子失败,请稍后重试！");
        }
    }

    // 删除帖子
    @DeleteMapping("delete_post")
    public Result deletePost(Integer id){
        if (postService.deletePost(id)){
            return Result.success();
        } else {
            return Result.error("删除帖子失败,请稍后重试！");
        }
    }

    // 获取帖子
    @GetMapping("get_post")
    public Result getPost(Integer id,@RequestHeader("X-Client-Type") String clientType){
        return Result.success(postService.getPostById(id,clientType));
    }

    // 更新帖子
    @PutMapping("update_post")
    public Result updatePost(@RequestBody Post post){
        if (postService.updatePost(post)){
            return Result.success();
        } else {
            return Result.error("更新帖子失败,请稍后重试！");
        }
    }
}
